package com.taotao.portal.controller;

import com.taotao.common.util.JsonUtils;
import com.taotao.content.service.ContentService;
import com.taotao.manager.pojo.TbContent;
import com.taotao.portal.pojo.Ad1Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class PageController {
    @Autowired
    private ContentService contentService;
    @Value("${AD1_CATEGORY_ID}")
    private Long categoryId;
    @Value("${AD1_HEIGHT}")
    private String AD1_HEIGHT;
    @Value("${AD1_HEIGHTB}")
    private String AD1_HEIGHTB;
    @Value("${AD1_WIDTH}")
    private String AD1_WIDTH;
    @Value("${AD1_WIDTHB}")
    private String AD1_WIDTHB;
    @RequestMapping("/index")
    public String showIndex(Model model){
        List<TbContent> contentList = contentService.getContentListByCategoryId(categoryId);
        List<Ad1Node> nodes = new ArrayList<>();
        for (TbContent content:contentList) {
            Ad1Node node = new Ad1Node();
            node.setAlt(content.getSubTitle());
            node.setHeight(AD1_HEIGHT);
            node.setHeightB(AD1_HEIGHTB);
            node.setHref(content.getUrl());
            node.setSrc(content.getPic());
            node.setSrcB(content.getPic2());
            node.setWidth(AD1_WIDTH);
            node.setWidthB(AD1_WIDTHB);
            nodes.add(node);
        }
        //传递数据到jsp
        model.addAttribute("ad1",JsonUtils.objectToJson(nodes));

        return "index";
    }
}
